package com.emenu;

import java.util.List;
import com.emenu.application.StaticObject;
import com.emenu.bean.PubTimer;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class FunTimeFragment extends Fragment {
	private TextView txttime;
	private Button btchange;
	public StaticObject SO;
	List<PubTimer> PubTimerlist;
	// 初始值，实际在onCreate时恢复上当前时间的市别
	String timecode = "1";

	// private int Hit_time_code = 1;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		SO = (StaticObject) activity.getApplication();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.funtime, container, false);

		findViewsById(v);

		setListener();

		for (PubTimer pt : SO.getPub_timerList()) {

			if (pt.getCode().equals(timecode)) {
				txttime.setText(pt.getName() + "");
			}
		}

		return v;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		PubTimerlist = SO.getPub_timerList();

		timecode = SO.getStaticTimeCode();

	}

	/** 视图 样式 */
	private void findViewsById(View v) {
		txttime = (TextView) v.findViewById((R.id.fun_time));
		btchange = (Button) v.findViewById((R.id.funtimechange));
	}

	/** 设置监听器 */
	private void setListener() {
		btchange.setOnClickListener(changeListener);

	}

	private OnClickListener changeListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			/***
			 * 点击切换后，每次加1，根据全局变量的值list长度自动判断点击后的市别， 午市（code=1），晚上（code=2）
			 **/
			timecode = (Integer.parseInt(timecode) + 1) + "";

			for (PubTimer pt : PubTimerlist) {
				// 每次点击切换timecode加1，timecode/总的市别记录数则为当前市别code
				int j = Integer.parseInt(timecode) % PubTimerlist.size();
				if (j == 0) {
					j = PubTimerlist.size();
				}

				if (j == Integer.parseInt(pt.getCode())) {
					txttime.setText(pt.getName());
					SO.setStaticTimeCode(j + "");
					break;
				}
			}

		}
	};

}
